package com.ovopark.organize.server.util;

import java.util.concurrent.*;

/**
 * 多线程处理器，分发任务
 */
@SuppressWarnings("all")
public class ThreadPoolTaskUtils {

  private static int CAPACITY = 10000;

  // 线程池核心线程数
  private static int CORE_POOL_SIZE = 400;

  // 线程池最大线程数
  private static int MAXIMUM_POOL_SIZE = 400;

  // 额外线程空状态生存时间
  private static Long KEEP_ALIVE_TIME = 0L;

  private static TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS;

  private static ExecutorService threadPool;

//  static {
//
//    BlockingQueue<Runnable> workingQueue = new ArrayBlockingQueue<Runnable>(CAPACITY);
//
//    RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.AbortPolicy();
//
//    threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, TIME_UNIT, workingQueue,
//        rejectedExecutionHandler);
//  }

  public static ExecutorService getThreadPool() {
    if (threadPool != null) {
      return threadPool;
    }
    return initPool(CORE_POOL_SIZE,MAXIMUM_POOL_SIZE,CAPACITY);
  }

  public synchronized static ExecutorService initPool(int core, int max, int capacity) {
    if (threadPool != null) {
      return threadPool;
    }
    CAPACITY = capacity;
    CORE_POOL_SIZE = core;
    MAXIMUM_POOL_SIZE = max;
    BlockingQueue<Runnable> WORK_QUEUE = //new LinkedBlockingQueue<Runnable>();
        new ArrayBlockingQueue<Runnable>(CAPACITY);
    RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.AbortPolicy();
    threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, TIME_UNIT, WORK_QUEUE,
        rejectedExecutionHandler);
    ExecutorService executorService = Executors.newCachedThreadPool();
    executorService.execute(() ->{});
    return threadPool;
  }

  /**
   * 线程池线程数量
   *
   * @return
   * @throws Exception
   */
  public static int poolSize() throws Exception {
    return ((ThreadPoolExecutor) threadPool).getPoolSize();
  }

  /**
   * 队列中等待执行的任务数目
   *
   * @return
   * @throws Exception
   */
  public static int waitExecuteQueueSize() throws Exception {
    return ((ThreadPoolExecutor) threadPool).getQueue().size();
  }

  /**
   * 已执行完成数目
   *
   * @return
   * @throws Exception
   */
  public static Long compelteTaskCount() throws Exception {
    return ((ThreadPoolExecutor) threadPool).getCompletedTaskCount();
  }

  /**
   * 需要执行的任务数目
   *
   * @return
   * @throws Exception
   */
  public static Long needExecuteCount() throws Exception {
    return ((ThreadPoolExecutor) threadPool).getTaskCount();
  }

  /**
   * 活跃总数
   *
   * @return
   * @throws Exception
   */
  public static int activeCount() throws Exception {
    return ((ThreadPoolExecutor) threadPool).getActiveCount();
  }

  /**
   * 等待线程执行时间
   *
   * @param seconds
   * @throws InterruptedException
   */
  public static void awaitTermination(int seconds) throws InterruptedException {
    threadPool.awaitTermination(seconds, TimeUnit.SECONDS);
  }

  /**
   * 提交任务
   *
   * @param runnable
   * @throws Exception
   */
  public static void submit(Runnable runnable) throws Exception {
    threadPool.submit(runnable);
  }
}
